package com.fieldchain.ex.fa100.utils.common;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.ImageView;

import com.fieldchain.ex.fa100.custom.view.MyAnimationUtils;
import com.fieldchain.ex.fa100.inter.IntegerCallBack;
import com.fieldchain.ex.fa100.utils.screen.Dputils;
import com.fieldchain.ex.fa100.utils.share.ShareManager;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/10
 */
public class InputAnimationTwo {
    private EditText mEt;
    private View mView;
    private ImageView mIvBac;
    private int height;
    private boolean inputShow;
    private ValueAnimator showAnimator,hidnAnimator,ivShowAnimator,ivHidnAnimation;
    private int width;

    public void attachView(final Activity mContext,  EditText et, View view,ImageView ivBac){
        this.height=Dputils.dp2px(70);
        mEt=et;
        mView=view;
        mIvBac=ivBac;
        width= ShareManager.getInstance().getWidth()- Dputils.dp2px(60);
        initAnimation();
        initListener(mContext);

    }

    public void detachView(){
        mEt=null;
        mView=null;
        if(hidnAnimator.isRunning()){
            hidnAnimator.cancel();
        }
        if(showAnimator.isRunning()){
            showAnimator.cancel();
        }
        hidnAnimator=null;
        showAnimator=null;
    }
    private void initListener(final Activity mContext){
        mEt.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            //当键盘弹出隐藏的时候会 调用此方法。
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                //获取当前界面可视部分
                mContext.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                //获取屏幕的高度
                int screenHeight = mContext.getWindow().getDecorView().getRootView().getHeight();
                //此处就是用来获取键盘的高度的， 在键盘没有弹出的时候 此高度为0 键盘弹出的时候为一个正数
                int heightDifference = screenHeight - r.bottom;

                if (heightDifference > 0) {
                    if(inputShow==true){
                        return;
                    }
                    inputShow=true;
                    showAnimator.start();
                    ivShowAnimator.start();
                } else {
                    if(inputShow==false){
                        return;
                    }
                    inputShow=false;
                    hidnAnimator.start();
                    ivHidnAnimation.start();
                }
            }
        });
    }

    public void initAnimation(){
        showAnimator = ValueAnimator.ofInt(height, 0);
        showAnimator.setDuration(1200);

        hidnAnimator = ValueAnimator.ofInt(0, height);
        hidnAnimator.setDuration(1200);

        ivShowAnimator = ValueAnimator.ofInt(width, width*2/3);
        ivShowAnimator.setDuration(1200);

        ivHidnAnimation = ValueAnimator.ofInt(width*2/3, width);
        ivHidnAnimation.setDuration(1200);
        final ViewGroup.LayoutParams params=mView.getLayoutParams();
        final ViewGroup.LayoutParams ivParams=mIvBac.getLayoutParams();
        MyAnimationUtils.setListener(showAnimator, new IntegerCallBack() {
            @Override
            public void index(int status) {
                params.height=status;
                mView.setLayoutParams(params);
            }
        });
        MyAnimationUtils.setListener(hidnAnimator, new IntegerCallBack() {
            @Override
            public void index(int status) {
                params.height=status;
                mView.setLayoutParams(params);
            }
        });

        Log.e("ttttt",mIvBac.getTop()+"");

        MyAnimationUtils.setListener(ivShowAnimator, new IntegerCallBack() {
            @Override
            public void index(int status) {
                ivParams.width=status;
                ivParams.height=status;
                mIvBac.setLayoutParams(ivParams);
            }
        });

        MyAnimationUtils.setListener(ivHidnAnimation, new IntegerCallBack() {
            @Override
            public void index(int status) {
                ivParams.width=status;
                ivParams.height=status;
                mIvBac.setLayoutParams(ivParams);
            }
        });
    }

}
